// HelloTriangle.js (c) 2012 matsuda
// Vertex shader gl.program
var VSHADER_SOURCE = `
attribute vec2 a_position1;
attribute vec2 a_position2;
attribute vec4 a_color;
varying vec4 v_color;

void main() {
gl_Position = vec4((a_position1 + a_position2).xy, 0, 1);
v_color = a_color;
}
`;

// Fragment shader gl.program
var FSHADER_SOURCE = `
precision mediump float;
varying vec4 v_color;
void main() {
gl_FragColor = v_color;
}
`;

function main() {
  // Retrieve <canvas> element
  var canvas = document.getElementById("webgl");

  // Get the rendering context for WebGL
  var gl = getWebGLContext(canvas);
  if (!gl) {
    console.log("Failed to get the rendering context for WebGL");
    return;
  }

  // Initialize shaders
  if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
    console.log("Failed to intialize shaders.");
    return;
  }

  // Write the positions of vertices to a vertex shader
  var n = initVertexBuffers(gl);
  if (n < 0) {
    console.log("Failed to set the positions of the vertices");
    return;
  }

  // Specify the color for clearing <canvas>
  gl.clearColor(0, 0, 0, 1);

  // Clear <canvas>
  gl.clear(gl.COLOR_BUFFER_BIT);

  // Draw the rectangle
  gl.drawArrays(gl.TRIANGLES, 0, n);
}

function initVertexBuffers(gl) {
  // look up where the vertex data needs to go.
  var positionAttributeLocation1 = gl.getAttribLocation(
    gl.program,
    "a_position1"
  );
  // Create a buffer and put three 2d clip space points in it
  var positionBuffer1 = gl.createBuffer();

  // Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = positionBuffer)
  gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer1);

  var positions = [-1, -1, 1, -1, 0, 0.5];

  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);

  // Tell WebGL how to convert from clip space to pixels
  gl.viewport(0, 0, 300, 300);

  // Clear the canvas
  gl.clearColor(0, 0, 0, 0.5);
  // gl.clear(gl.COLOR_BUFFER_BIT);
  gl.clear(gl.COLOR_BUFFER_BIT);

  // Tell it to use our gl.program (pair of shaders)
  gl.useProgram(gl.program);

  // Turn on the attribute
  gl.enableVertexAttribArray(positionAttributeLocation1);

  // Bind the position buffer.
  // gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);

  // Tell the attribute how to get data out of positionBuffer (ARRAY_BUFFER)
  var size = 2; // 2 components per iteration
  var type = gl.FLOAT; // the data is 32bit floats
  var normalize = false; // don't normalize the data
  var stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next position
  var offset = 0; // start at the beginning of the buffer
  gl.vertexAttribPointer(
    positionAttributeLocation1,
    size,
    type,
    normalize,
    stride,
    offset
  );

  // look up where the vertex data needs to go.
  var positionAttributeLocation2 = gl.getAttribLocation(
    gl.program,
    "a_position2"
  );

  // Create a buffer and put three 2d clip space points in it
  var positionBuffer2 = gl.createBuffer();
  // Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = positionBuffer)
  gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer2);

  var positions = [0, 0, 0, 0, 0, 0.5];
  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);

  // Turn on the attribute
  gl.enableVertexAttribArray(positionAttributeLocation2);

  // Bind the position buffer.
  // gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);

  // Tell the attribute how to get data out of positionBuffer (ARRAY_BUFFER)
  var size = 2; // 2 components per iteration
  var type = gl.FLOAT; // the data is 32bit floats
  var normalize = false; // don't normalize the data
  var stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next position
  var offset = 0; // start at the beginning of the buffer
  gl.vertexAttribPointer(
    positionAttributeLocation2,
    size,
    type,
    normalize,
    stride,
    offset
  );

  // look up where the vertex data needs to go.
  var positionAttributeColor = gl.getAttribLocation(gl.program, "a_color");

  // Create a buffer and put three 2d clip space points in it
  var colorBuffer = gl.createBuffer();
  // Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = positionBuffer)
  gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);

  var colors = [0.5, 0.0, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.0, 0.5, 0.5];
  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);

  // Turn on the attribute
  gl.enableVertexAttribArray(positionAttributeColor);

  // Bind the position buffer.
  // gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);

  // Tell the attribute how to get data out of positionBuffer (ARRAY_BUFFER)
  var size = 4; // 2 components per iteration
  var type = gl.FLOAT; // the data is 32bit floats
  var normalize = false; // don't normalize the data
  var stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next position
  var offset = 0; // start at the beginning of the buffer
  gl.vertexAttribPointer(
    positionAttributeColor,
    size,
    type,
    normalize,
    stride,
    offset
  );
  return 3;
}
